package at.fhj.swd.pse.beans;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.enterprise.inject.Produces;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.logging.Logger;
import org.primefaces.model.DefaultUploadedFile;

import at.fhj.swd.pse.entities.User;
import at.fhj.swd.pse.helper.UserManager;

@Named
@Stateless
@ManagedBean
public class UserBean {
	
	private static final Logger logger = Logger.getLogger(UserBean.class);

	@Inject
	private UserManager userManager;

	@Named
	private User profileData;
	
	@Named
	private DefaultUploadedFile uploadedFile;
	
	public UserBean() {

	}

	@PostConstruct
	public void init() {
		profileData = userManager.getActiveUser();
		
	}

	public User getUserProfileByUser(final User user) {
		return userManager.getUserProfileByUserId(user.getId());
	}

	public User getActiveUSer() {
		return userManager.getActiveUser();
	}

	public void updateUser() {
		
		logger.info("UPDATE USER");
		logger.info("Update profile of user " + profileData.getUsername());
		
		userManager.updateUser(profileData);
		
	}
	
	public void uploadUserProfileImage() {
		
		logger.info("Upload Profile Image of user " + profileData.getUsername());
		profileData.setImage(uploadedFile.getContents());
		userManager.updateUser(profileData);
		
	}

	@Produces
	public User getProfileData() {
		return this.profileData;
	}

	public void setProfileData(User profileData) {
		this.profileData = profileData;
	}

	@Produces
	public DefaultUploadedFile getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(DefaultUploadedFile uploadedFile) {
		this.uploadedFile = uploadedFile;
	}
	
}
